package com.example.taobaolianmeng.ui.fragment;

import android.graphics.Rect;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaolianmeng.R;
import com.example.taobaolianmeng.base.BaseFragment;
import com.example.taobaolianmeng.model.domain.Histories;
import com.example.taobaolianmeng.model.domain.SearchRecommend;
import com.example.taobaolianmeng.model.domain.SearchResult;
import com.example.taobaolianmeng.presenter.impl.SearchPagePresenterImpl;
import com.example.taobaolianmeng.ui.adapter.HomePageContentAdapter;
import com.example.taobaolianmeng.ui.custom.TextFlowLayout;
import com.example.taobaolianmeng.utils.PresenterManger;
import com.example.taobaolianmeng.utils.SizeUtils;
import com.example.taobaolianmeng.view.ISearchPageCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SearchFragment extends BaseFragment implements ISearchPageCallback {

    private static final String TAG = "SearchFragment";
    private SearchPagePresenterImpl mSearchPagePresenter;

    @BindView(R.id.search_history_view)
    public TextFlowLayout mHistoriesView;

    @BindView(R.id.search_recommend_view)
    public TextFlowLayout mRecommendView;

    @BindView(R.id.search_recommend_container)
    public View mRecommendContainer;

    @BindView(R.id.search_history_container)
    public View mHistoriesContainer;

    @BindView(R.id.search_history_delete)
    public View mHistoriesDelete;

    @BindView(R.id.search_result_list)
    public RecyclerView mSearchList;

    private HomePageContentAdapter mSearchResultAdapter;


    @Override
    protected void initPresenter() {
        mSearchPagePresenter = PresenterManger.getInstance().getSearchPagePresenter();
        mSearchPagePresenter.registerViewCallback(this);
        //获取搜索推荐词
        mSearchPagePresenter.getRecommendWords();
        mSearchPagePresenter.doSearch("毛衣");
        mSearchPagePresenter.getHistories();
    }


    @Override
    protected void release() {
        mSearchPagePresenter.unregisterViewCallback(this);
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragement_search_layout,container,false);
    }

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_search;
    }
    @Override
    protected void initView(View rootView) {
        //设置布局管理器
        mSearchList.setLayoutManager(new LinearLayoutManager(getContext()));
        //设置适配器
        mSearchResultAdapter = new HomePageContentAdapter();
        mSearchList.setAdapter(mSearchResultAdapter);

    }

    @Override
    protected void initListener() {
        mHistoriesDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清空历史
                mSearchPagePresenter.delHistories();
            }
        });
    }

    @Override
    public void onHistoriesLoaded(Histories histories) {
        Log.d(TAG,"histories->"+histories);
        if(histories==null || histories.getHistories().size() ==0){
            mHistoriesContainer.setVisibility(View.GONE);
        }else{
            mHistoriesContainer.setVisibility(View.VISIBLE);
            mHistoriesView.setTextList(histories.getHistories());
        }
    }

    @Override
    public void onHistoriesDeleted() {
        //跟新历史记录
        if(mSearchPagePresenter !=null){
            mSearchPagePresenter.getHistories();
        }
    }

    @Override
    public void onSearchSuccess(SearchResult result) {
        Log.d(TAG,"onSearchSuccess->"+result);
        setupState(State.SUCCESS);
        //隐藏掉历史记录和推荐
        mRecommendContainer.setVisibility(View.GONE);
        mHistoriesContainer.setVisibility(View.GONE);
        //显示搜索结果
        mSearchList.setVisibility(View.VISIBLE);
        //设置数据
        mSearchResultAdapter.setData(result.getData().getTbk_dg_material_optional_response().getResult_list().getMap_data());
        mSearchList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = SizeUtils.dip2px(getContext(),1.5f);
                outRect.bottom = SizeUtils.dip2px(getContext(),1.5f);
            }
        });
    }

    @Override
    public void onMoreLoaded(SearchResult result) {

    }

    @Override
    public void onMoreLoadedError() {

    }

    @Override
    public void onMoreLoadedEmpty() {

    }

    @Override
    public void onRecommendWordsLoaded(List<SearchRecommend.DataBean> recommendWords) {
        //推荐数据
        Log.d(TAG,"recommendWords size ->"+recommendWords.size());
        List<String> recommendKeywords = new ArrayList<>();
        for (SearchRecommend.DataBean item : recommendWords) {
            recommendKeywords.add(item.getKeyword());
        }
        if(recommendWords == null || recommendWords.size() ==0){
            mRecommendContainer.setVisibility(View.GONE);
        }else{
            mRecommendView.setTextList(recommendKeywords);
            mRecommendContainer.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onNetworkError() {

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onEmpty() {

    }
}
